package com.dora.basic.juc;

import java.util.Scanner;
import java.util.concurrent.RecursiveTask;

/**
 * 斐波那契任务
 *
 * @author hanchen
 * @date 2024/02/21
 */
public class ForkJoinFibonacciTask extends RecursiveTask<Integer> {
    private final int n;

    public ForkJoinFibonacciTask(int n) {
        this.n = n;
    }

    @Override
    protected Integer compute() {
        if (n <= 1) {
            return n;
        } else {
            ForkJoinFibonacciTask f1 = new ForkJoinFibonacciTask(n - 1);
            f1.fork();
            ForkJoinFibonacciTask f2 = new ForkJoinFibonacciTask(n - 2);
            return f2.compute() + f1.join();
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int calcuteValue = scanner.nextInt();
        ForkJoinFibonacciTask task = new ForkJoinFibonacciTask(calcuteValue);
        int result = task.compute();
        System.out.println("Fibonacci(" + calcuteValue + ") = " + result);
    }
}
